package com.example.xiaomaotv;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import com.example.xiaomaotv.adapter.MsgAdapter;
import com.example.xiaomaotv.entity.Msg;

import java.util.ArrayList;
import java.util.List;

public class MainActivity4 extends AppCompatActivity {

    private List<Msg> msgData = new ArrayList<>();
    private MsgAdapter msgAdapter;
    private ListView msglist;
    private EditText msgEdit;
    private String receiverName,content;
    private SharedPreferences sp;
    private int imgResId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        sp =getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        ImageView backBtn = findViewById(R.id.back_btn);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 获取传递的意图数据
        Intent intent = getIntent();
        if (intent != null) {
            receiverName = intent.getStringExtra("Name");
            imgResId = intent.getIntExtra("Img", R.drawable.bac_login);
            content = intent.getStringExtra("Content");
           // 设置聊天对方信息
            TextView nameText = findViewById(R.id.name_text);
            ImageView animeImg = findViewById(R.id.anime_img);
            TextView briefText = findViewById(R.id.brief_text);
            nameText.setText(receiverName);
            animeImg.setImageResource(imgResId);
            briefText.setText(receiverName + " 在线中");
        }
        // 初始化消息数据
        initMsgData();
        // 初始化ListView和适配器
        msgAdapter = new MsgAdapter(MainActivity4.this, R.layout.item_msg, msgData);
        msglist = findViewById(R.id.list_msg);
        msglist.setAdapter(msgAdapter);
        // 初始化输入控件
        msgEdit = findViewById(R.id.send_edit);
        Button sendbtn = findViewById(R.id.send_btn);
        // 设置发送按钮点击事件
        sendbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
            }
        });
    }
    // 初始化消息数据
    private void initMsgData() {
        if (receiverName != null) {
            Msg welcomeMsg = new Msg(content,imgResId, Msg.TYPE_RECEIVED);
            msgData.add(welcomeMsg);
        }
    }
    // 发送消息方法
    private void sendMessage() {
        String content = msgEdit.getText().toString().trim();
        String SaveTitle = sp.getString("title", "");
        int avatarResId = R.drawable.title;
        if (!content.isEmpty()) {
            if (!SaveTitle.isEmpty()) {
                try {
                    avatarResId = getResources().getIdentifier(SaveTitle, "drawable", getPackageName());
                } catch (Exception e) {
                    e.printStackTrace();
                    avatarResId = R.drawable.title;
                }
            }
            // 创建新消息（类型为发送）
            Msg newMsg = new Msg(content, avatarResId, Msg.TYPE_SENT);
            msgData.add(newMsg);
            // 通知适配器数据变化并滚动到底部
            msgAdapter.notifyDataSetChanged();
            msglist.smoothScrollToPosition(msgData.size() - 1);
            // 清空输入框
            msgEdit.setText("");
        } else {
            Toast.makeText(this, "消息内容不能为空", Toast.LENGTH_SHORT).show();
        }
    }
}